//
//  MessageViewController.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/24.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//



#import "MessageViewController.h"
#import "XJYPaggingNavBar.h"
#import "XJYHeader.h"

#import "ChatTableViewController.h"
//三个子视图控制器
#import "NotificationTableViewController.h"
#import "CommentTableViewController.h"

#import "LCChatKitExample.h"
#import "LCCKUtil.h"
#import "LCCKTabBarControllerConfig.h"

#import "AppDelegate.h"
@interface MessageViewController ()<UIScrollViewDelegate,UIActionSheetDelegate>

/**
 * scrollView
 */
@property (nonatomic,strong) UIScrollView *scrollView;
/**
 *  显示title集合的容器
 */
@property (nonatomic, strong) XJYPaggingNavBar *paggingNavbar;
/**
 *  标识当前页码
 */
@property (nonatomic, assign) NSInteger currentPage;



@end



@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark Chat
//    [self setupChatKit];
    [self setupAllChildViewControllers];
    [self setupScrollView];
    [self addAllChildViewsInScrollView];
    [self setupPaggingNavbar];
    [self setupNavigationBar];
}

//进入消息界面，就登陆聊天
- (void)setupChatKit{
    [LCCKUtil showProgressText:@"open client ..." duration:10.0f];
    [LCChatKitExample invokeThisMethodAfterLoginSuccessWithClientId:@"Tom" success:^{
        [LCCKUtil hideProgress];
        [LCCKUtil hideProgress];
        LCCKTabBarControllerConfig *tabBarControllerConfig = [[LCCKTabBarControllerConfig alloc] init];
        
        [[UIApplication sharedApplication] windows][0].rootViewController = tabBarControllerConfig.tabBarController;
    } failed:^(NSError *error) {
        [LCCKUtil hideProgress];
        
    }];
}

- (void)setupAllChildViewControllers{
    
    [self addChildViewController:[[NotificationTableViewController alloc]init]];
    self.childViewControllers[0].title = @"通知";
    
    [self addChildViewController:[[CommentTableViewController alloc]init]];
    self.childViewControllers[1].title = @"评论";
    
    [self addChildViewController:[[ChatTableViewController alloc]init]];
    self.childViewControllers[2].title = @"聊天";
    
}

- (void)setupScrollView{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    self.scrollView.delegate = self;
    
    NSUInteger count = self.childViewControllers.count;
    CGFloat scrollViewW = self.view.frame.size.width;
    
    scrollView.contentSize = CGSizeMake(count * scrollViewW, 0);
    
}

- (void)addChildVcViewIntoScrollView:(NSUInteger)index{
    UIViewController *childVc = self.childViewControllers[index];
    
    if(childVc.isViewLoaded) return;
    
    UIView *childVcView = childVc.view;
    
    CGFloat scrollViewW = ScreenW;
    childVcView.frame = CGRectMake(index * scrollViewW, 0, ScreenW, ScreenH);
    
    
    [self.scrollView addSubview:childVcView];
}


- (void)addAllChildViewsInScrollView{
    [self addChildVcViewIntoScrollView:0];
    [self addChildVcViewIntoScrollView:1];
    [self addChildVcViewIntoScrollView:2];
}

- (void)setupPaggingNavbar{
    //设置titleView
    self.navigationItem.titleView = self.paggingNavbar;
    //设置paggingNavbar当前页面
    self.paggingNavbar.currentPage = 0;
    //设置标题
    self.paggingNavbar.titles = @[self.childViewControllers[0].title,self.childViewControllers[1].title,self.childViewControllers[2].title];
    //设置初始偏移量
    //self.paggingNavbar.contentOffset = CGPointMake(375, 0);
    //paggingNavbar 加载数据 也就是标题喽
    [self.paggingNavbar reloadData];
}

- (void)setupNavigationBar{

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"MoreIcon-Small"] style:UIBarButtonItemStylePlain target:self action:@selector(MoreButtonClicked:) ];
    
    rightBarButtonItem.tintColor = [UIColor lightGrayColor];
    
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)MoreButtonClicked:(UIBarButtonItem *)rightBarButtonItem{
    NSLog(@"leftBarButtonItem Clicked");
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"取消", nil)                                         destructiveButtonTitle:NSLocalizedString(@"清空消息", nil)
                                              otherButtonTitles:NSLocalizedString(@"全部设为已读", nil) ,nil];
    
    [sheet showInView:self.view];
}




//  得到当前的 currentPage
- (NSInteger)currentPage {
    long currentPage = lround( _scrollView.contentOffset.x / CGRectGetWidth(_scrollView.frame) );
    return currentPage;
}
#pragma mark - ScrollView Events -
//开始滚动调用该方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)aScrollView {
    
}
//在滚动视图中拖动结束时通知代理。
//滚动视图在用户的手指在拖动内容之后触摸时发送此消息。 UIScrollView的减速属性控制减速。
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    
}

//告诉代表滚动视图已经结束减速滚动运动。
//当滚动移动停止时，滚动视图调用此方法。 UIScrollView的减速属性控制减速。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    long currentPage = self.currentPage;
    self.currentPage = currentPage;
    self.paggingNavbar.currentPage = currentPage;
    
}


//当用户滚动接收器中的内容视图时告诉代表。
//委托通常实现这种方法来获取来自scrollView的内容偏移的变化，并绘制内容视图的受影响部分。
//拖动过程中一直调用。
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.paggingNavbar.contentOffset = scrollView.contentOffset;
    
}

//paggingNavbar 实际上是  titleView
- (XJYPaggingNavBar *)paggingNavbar {
    if (!_paggingNavbar) {
        _paggingNavbar = [[XJYPaggingNavBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) / 2.0, 44)];
        _paggingNavbar.backgroundColor = [UIColor clearColor];
    }
    return _paggingNavbar;
}




@end
